<?php

import('@.Model.Extend.EventViewModel');
import('@.Model.Where.EventWhereMachine');
class EventAction extends Action{
    static $COUNT_PER_PAGE=15;
    public function index(){
        $page= $this->getPageNum();
        $e= $this->getEvents($page);
        $this->assign('events', $e);
        $this->display();
    }
    public function search(){
        $ev= new EventViewModel();
        $map= new EventWhereMachine();
        //将role值转为正确的数值
        if ($role=$_GET['role']){
            if (! is_numeric($role)){
                $role= strtolower($role);
                if (strpos($role, 'user')!==false){
                    $role= AccountModel::C('ROLE_USER_BASIC');
                } else if (strpos($role, 'group')!==false){
                    $role= AccountModel::C('ROLE_GROUP');
                }
            }
        }
        if (! is_null($role)){
            $map->accountRole($role);
            $this->assign('role', $role);
        }
        if (! is_null($type=$_GET['type'])){
            $map->type($type);
        }
        if (! is_null($q=$_GET['q'])){
            $map->word($q);
        }
        //分页
        import("@.Common.NoticePage");
        $count=$ev->where($map->get())->count();
        $page= new NoticePage($count, self::$COUNT_PER_PAGE);
        $this->assign('page', $page->show());
        
        $e= $ev->limit($page->firstRow.','.$page->listRows)->search($map->get());
        $this->assign('events', $e);
        
        $this->display('index');
    }
    /****
    * @id 指定活动id，可以为数组
    * @state 活动新值
    */
    public function state(){
        if (! $_REQUEST['submit']){
            return;
        }
        $id= $_REQUEST['id'];
        //ThinkPHP Db.class 将自动处理IN的数组转换
        $map=array('id'=> array('IN', $id));
        $em= new EventModel();
        $state= $em->checkState($_REQUEST['state']);    //校验state
        $res= $em->where($map)->setField('visibility', $state);
        
        if (!$res){
            $this->error(array('message'=>"failed"));
        } else{
            $this->success(array('message'=>$res));
        }
    }
    /****
    * 删除
    * @id 指定活动id，可以为数组
    */
    public function del(){
        if (! $_REQUEST['submit']){
            return;
        }
        $id= $_REQUEST['id'];
        $map=array('id'=> array('IN', $id));
        $em= new EventModel();
        $res= $em->where($map)->delete();
        
        if (!$res){
            $this->error(array('message'=>"操作失败"));
        } else{
            $this->success(array('message'=>$res));
        }
    }
}

?>